﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using VX.DBTransfer.BLL.Common;
using VX.DBTransfer.BLL.Transfer;
using VX.DBTransfer.Framework;
using VX.DBTransfer.Model;
using VX.DBTransfer.Model.Entity;

namespace VX.DBTransfer.Window
{
    public partial class MainFrame : Form
    {
        public MainFrame()
        {
            InitializeComponent();
        }

        private void btn_SaveConfig_Click(object sender, EventArgs e)
        {
            CommonManage _manage = new CommonManage();
            DBConfigModel sourceModel = new DBConfigModel()
            {
                IP = txt_SourceIP.Text,
                DBName = txt_SourceDBName.Text,
                Login = txt_SourceLogin.Text,
                Password = txt_SourcePassword.Text,
                Schema = txt_SourceSchema.Text
            };
            DBConfigModel targetModel = new DBConfigModel()
            {
                IP = txt_TargetIP.Text,
                DBName = txt_TargetDBName.Text,
                Login = txt_TargetLogin.Text,
                Password = txt_TargetPassword.Text,
                Schema = txt_TargetSchema.Text
            };
            DBConfigModel customerModel = new DBConfigModel()
            {
                IP = txt_CustomerIP.Text,
                DBName = txt_CustomerDBName.Text,
                Login = txt_CustomerLogin.Text,
                Password = txt_CustomerPassword.Text
            };
            bool result = true;
            //1.确认vxCustomer配置是否正确
            if (!_manage.IsRightDBConfig(customerModel))
            {
                MessageBox.Show("vxCustomer连接失败", "错误");
                result = false;
            }
            //2.确认源Schema配置是否正确
            if (!_manage.IsRightDBConfig(sourceModel))
            {
                MessageBox.Show("源Schema连接失败", "错误");
                result = false;
            }
            //3.确认目标Schema配置是否正确
            if (!_manage.IsRightDBConfig(targetModel))
            {
                MessageBox.Show("目标Schema连接失败", "错误");
                result = false;
            }
            btn_Transfer.Enabled = result;
            cbx_CoverTargetSchema.Enabled = result;
        }

        private void btn_Transfer_Click(object sender, EventArgs e)
        {
            txt_Result.Clear();
            DBConfigModel sourceModel = new DBConfigModel()
            {
                IP = txt_SourceIP.Text,
                DBName = txt_SourceDBName.Text,
                Login = txt_SourceLogin.Text,
                Password = txt_SourcePassword.Text,
                Schema = txt_SourceSchema.Text
            };
            DBConfigModel targetModel = new DBConfigModel()
            {
                IP = txt_TargetIP.Text,
                DBName = txt_TargetDBName.Text,
                Login = txt_TargetLogin.Text,
                Password = txt_TargetPassword.Text,
                Schema = txt_TargetSchema.Text
            };
            DBConfigModel customerModel = new DBConfigModel()
            {
                IP = txt_CustomerIP.Text,
                DBName = txt_CustomerDBName.Text,
                Login = txt_CustomerLogin.Text,
                Password = txt_CustomerPassword.Text
            };
            var progressReporter = new ProgressReporter();
            Action<string> action = AlertResult;
            Task t = new Task(() => NewTask(progressReporter, sourceModel, targetModel, customerModel, cbx_CoverTargetSchema.Checked, action));
            t.Start();
        }

        private void ConfigTextChanged(object sender, EventArgs e)
        {
            btn_Transfer.Enabled = false;
        }

        private static void NewTask(ProgressReporter reporter, DBConfigModel sourceModel, DBConfigModel targetModel, DBConfigModel customerModel, bool IsCoverTargetSchema, Action<string> action = null)
        {
            Action<string> _alert = new Action<string>((info) =>
             {
                 if (action != null)
                     reporter.ReportProgress(() =>
                     {
                         action(info);
                     });
             });
            _alert("正在初始化操作...\r\n");
            SchemaTransferManage _manage = new SchemaTransferManage();
            //1.检查源表数量
            _alert("正在扫描源Schema表数量...\r\n");
            List<TableEntity> sourceTableLsit = _manage.LoadSchemaAllTables(sourceModel);
            Thread.Sleep(1000);
            _alert(string.Format("扫描完成，源Schema表共{0}个\r\n", sourceTableLsit.Count));
            //2.检查目标表数量
            _alert("正在扫描目标Schema表数量...\r\n");
            List<TableEntity> targetTableLsit = _manage.LoadSchemaAllTables(targetModel);
            Thread.Sleep(1000);
            _alert(string.Format("扫描完成，目标Schema表共{0}个\r\n", targetTableLsit.Count));
            //2.5. 如果数量不匹配，是否继续，继续则覆盖目标Schema
            bool isContinue = true;
            if (sourceTableLsit.Count != targetTableLsit.Count)
            {
                isContinue = false;
                if (!IsCoverTargetSchema)
                {
                    _alert("源Schema与目标Schema表数量不一致，数据迁移中断，请检查数据结构后重试。");
                }
                else
                {
                    //进行数据结构覆盖
                    isContinue = true;
                }
            }
            if (isContinue)
            {
                //3.检查源表结构
                _alert("正在扫描源Schema表结构...\r\n");
                Thread.Sleep(1000);
                //4.检查目标表结构
                _alert("正在扫描目标Schema表结构...\r\n");
                Thread.Sleep(1000);
                _alert("正在对比表结构...\r\n");
                //5.如果表数量不同或者表结构不同，是否继续，继续则覆盖目标Schema
                //bool isSame = TableEntity.IsTablesSame(sourceTableLsit, targetTableLsit);
                List<TableEntity> entitys = TableEntity.SelectDifferentTables(sourceTableLsit, targetTableLsit);
                if (entitys.Count > 0)
                {
                    isContinue = false;
                    if (!IsCoverTargetSchema)
                    {
                        _alert("源Schema与目标Schema表结构不一致，数据迁移中断，请检查数据结构后重试。");
                    }
                    else
                    {
                        //进行数据结构覆盖
                        isContinue = true;
                    }
                }
                Thread.Sleep(1000);
                if (isContinue)
                {
                    //6.数据迁移
                    _alert("表结构一致，准备进行数据迁移...\r\n");
                    //6.5 迁移失败
                    //txt_Result.Text += "迁移失败\r\n";
                    Thread.Sleep(1000);
                    //7.调整vxCustomer记录
                    _alert("数据迁移成功，正在调整vxCustomer配置...\r\n");
                    Thread.Sleep(1000);
                    //7.5 调整vxCustomer记录失败
                    //txt_Result.Text += "调整vxCustomer记录失败\r\n";
                    _alert("调整vxCustomer记录成功\r\n");
                }
            }
        }
        private void AlertResult(string info)
        {
            txt_Result.AppendText(info);
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
            InitFormData();
        }

        private void InitFormData()
        {
            txt_CustomerDBName.Text = "vxCustomer";
            txt_CustomerIP.Text = "10.55.160.101";
            txt_CustomerLogin.Text = "sa";
            txt_CustomerPassword.Text = "P@ssw0rd";
            txt_SourceDBName.Text = "vxTalent_qatest";
            txt_SourceIP.Text = "10.55.160.101";
            txt_SourceLogin.Text = "sa";
            txt_SourcePassword.Text = "P@ssw0rd";
            txt_SourceSchema.Text = "BCLQY";
            txt_TargetDBName.Text = "vxTalent_New";
            txt_TargetIP.Text = "10.55.160.101";
            txt_TargetLogin.Text = "sa";
            txt_TargetPassword.Text = "P@ssw0rd";
            txt_TargetSchema.Text = "ZAFAA";
        }
    }
}
